<?php

$db = new Database();
$auth = Auth::getAuth('current_user');
$search = new Search();

if(isset($_POST['action'])){
    if($_POST['action']=="getRequestUsers"){
        $form_id = $_POST['form_id'];
        echo getUsers_requestViewer($form_id);
    }else if($_POST['action']=="getRequestViewer"){
        $request_id = $_POST['request_id'];
        $form_id = $_POST['form_id'];
        $query = $this->query("SELECT u.id as id, CONCAT_WS(' ',u.first_name,u.last_name) as name FROM tbrequest_viewer rv LEFT JOIN tbuser u on rv.user_id = u.id WHERE request_id = '$request_id' AND form_id = '$form_id'","array");
        echo json_encode($query);
    }else if($_POST['action']=="saveViewer"){
        $array_search = $_POST['array_search'];
        $form_id = $_POST['form_id'];
        $requestId = $_POST['requestId'];

        //Delete
        $deletCond = array("request_id"=>$requestId,
                        "form_id"=>$form_id);
        $db->delete("tbrequest_viewer",$deletCond);
        foreach ($array_search as $value) {
            $insertVal = array("user_id"=>$value,
                            "request_id"=>$requestId,
                            "form_id"=>$form_id);
            $db->insert("tbrequest_viewer",$insertVal);
        }
    }else if($_POST['action']=="getTaggedUsers"){
        $request_id = $_POST['requestId'];
        $form_id = $_POST['form_id'];
        $numrows = $this->query("SELECT * FROM tbrequest_viewer WHERE request_id = '$request_id' AND form_id = '$form_id' and user_id = '". $auth['id'] ."'","numrows");
        echo $numrows;
    }else if($_POST['action']=="removetagRequest"){
        $request_id = $_POST['requestId'];
        $form_id = $_POST['form_id'];
        //Delete
        $deletCond = array("request_id"=>$request_id,
                        "form_id"=>$form_id,
                        "user_id"=>$auth['id']);
        $db->delete("tbrequest_viewer",$deletCond);

        $obj = array("condition"=>" AND form.ID = ". $request_id ."");
        $obj = json_decode(json_encode($obj),true);
        $result = $search->getManyRequest("",0,$form_id,0,1,$obj);
        echo count($result);
        // echo functions::base_encode_decode("encrypt",$form_id);
    }else if($_POST['action']=="getUsers"){
        $company_id = $auth['company_id'];
        $getUsers = $db->query("SELECT id, CONCAT_WS(' ',first_name,last_name) as name, first_name, last_name FROM tbuser WHERE company_id = '$company_id'","array");
        $ret = array();
        foreach ($getUsers as $value) {
            array_push($ret, array("user_image"=>post::avatarPic("tbuser",$value['id'],"30","30","small","avatar"),"id"=>$value['id'],"name"=>$value['name'],"first_name"=>$value['first_name'],"last_name"=>$value['last_name']));
        }
        echo json_encode($ret);
    }

}else{
    if(isset($_GET['seeAllUsersAddViewers'])){
        $form_id = $_GET['form_id'];
        $getUsers = json_decode(getUsers_requestViewer($form_id),true);
        echo '<link rel="stylesheet" type="text/css" href="/css/mention.css">';
        echo '<link rel="stylesheet" type="text/css" href="/css/user_css/stylesheets/structure.css">';
        echo '<div class="mentionContainer_only"><div class="mentionSearchChoice" style="margin-top: 0px;">';
        foreach ($getUsers as $value) {
            echo '<div class="mentionList" data-id="'. $value['id'] .'" style="cursor: auto;"><i class="icon-caret-right"> </i><span class="mentionList_txt">'. $value['name'] .'</span></div>';
        }
        echo '</div></div>';
    }
}

function getUsers_requestViewer($form_id){
    $db = new Database();
    $auth = Auth::getAuth('current_user');
    $search = new Search();
    $post = new post();

    $company_id = $auth['company_id'];
    $getUsers = $db->query("SELECT id, CONCAT_WS(' ',first_name,last_name) as name, first_name, last_name FROM tbuser WHERE company_id = '$company_id' and is_active = 1","array");
    // $getUsers = $search->getAllUsers();
    $ret = array();
    
    $forms_array = $db->query("SELECT form_json
                                      FROM tb_workspace
                                      WHERE id =  '$form_id'","row");
    $json =  json_decode($forms_array['form_json'],true);
    $categoryID = $json['categoryName'];
    if($categoryID==0 || $categoryID==""){
        $ret = $getUsers;
    }else{
        $getcategory = $db->query("SELECT * FROM tbform_category WHERE id = $categoryID","row");
        foreach ($getUsers as $value) {
            if($search->getFormPrivacyOtherUsers($getcategory['users'],$value['id'])>0){
                array_push($ret, array("user_image"=>$post->avatarPic("tbuser",$value['id'],"30","30","small","avatar"),"id"=>$value['id'],"name"=>$value['name'],"first_name"=>$value['first_name'],"last_name"=>$value['last_name']));
            }
        }
    }
    return json_encode($ret);
    // return "asd";
    // print_r($json['form_json']);
    // $search->getFormPrivacyOtherUsers($getcategory['users'],"1");
}

?>